package com.kxmall.web.controller.huishouapp;

import cn.dev33.satoken.annotation.SaIgnore;
import com.kxmall.address.domain.bo.KxAddressBo;
import com.kxmall.categorize.domain.vo.HsWasteCategorizeVo;
import com.kxmall.categorize.service.IHsWasteCategorizeService;
import com.kxmall.common.annotation.RepeatSubmit;
import com.kxmall.common.core.controller.BaseAppController;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.domain.R;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.validate.AddGroup;
import com.kxmall.common.core.validate.EditGroup;
import com.kxmall.huishou.domain.bo.HsWasteSiteBo;
import com.kxmall.huishou.domain.vo.HsWasteSiteVo;
import com.kxmall.huishou.service.IHsWasteSiteService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 回收站点
 *
 * @author lf
 * @date 2023-12-01
 */

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/site/app")
public class HsAppSiteController extends BaseAppController {

    private final IHsWasteSiteService iHsWasteSiteService;

    /**
     * 查询回收站点列表
     */
    @SaIgnore
    @GetMapping("/allList")
    public TableDataInfo<HsWasteSiteVo> getSitelist(HsWasteSiteBo bo, PageQuery pageQuery) {

        if(bo == null || bo.getLatitude() == null || bo.getLongitude() == null){

            return iHsWasteSiteService.queryPageList(bo, pageQuery);

        }else {

            List<HsWasteSiteVo> dataList = iHsWasteSiteService.queryPageListByLatAndLon(bo, pageQuery);
            if(dataList != null){
                for (HsWasteSiteVo hsWasteSiteVo : dataList){
                    if(hsWasteSiteVo.getDistance() == null || hsWasteSiteVo.getDistance().compareTo(BigDecimal.ZERO) <= 0){
                        continue;
                    }
                    if(hsWasteSiteVo.getDistance().compareTo(new BigDecimal(20)) >0){
                        hsWasteSiteVo.setDistanceStr(hsWasteSiteVo.getDistance().setScale(0, BigDecimal.ROUND_DOWN)+"公里");
                        hsWasteSiteVo.setArrivedTime("建议驾车前往");
                    }else if(hsWasteSiteVo.getDistance().compareTo(new BigDecimal(3)) >0){
                        hsWasteSiteVo.setDistanceStr(hsWasteSiteVo.getDistance().setScale(1, BigDecimal.ROUND_DOWN)+"公里");
                        BigDecimal minutes = hsWasteSiteVo.getDistance().divide(new BigDecimal(0.3),0,BigDecimal.ROUND_DOWN);
                        hsWasteSiteVo.setArrivedTime("骑行约"+minutes.longValue()+"分钟");

                    }else {

                        if(hsWasteSiteVo.getDistance().compareTo(new BigDecimal(1)) >=0){
                            hsWasteSiteVo.setDistanceStr(hsWasteSiteVo.getDistance().setScale(1, BigDecimal.ROUND_DOWN)+"公里");
                            BigDecimal minutes = hsWasteSiteVo.getDistance().divide(new BigDecimal(0.1),0,BigDecimal.ROUND_DOWN);
                            hsWasteSiteVo.setArrivedTime("步行约"+minutes.longValue()+"分钟");
                        }else {
                            long distanceStrNum = hsWasteSiteVo.getDistance().multiply(new BigDecimal(1000)).setScale(0, BigDecimal.ROUND_DOWN).longValue();
                            hsWasteSiteVo.setDistanceStr(distanceStrNum+"米");
                            BigDecimal minutes = hsWasteSiteVo.getDistance().divide(new BigDecimal(0.085),0,BigDecimal.ROUND_DOWN);
                            if(minutes.longValue() <= 0){
                                if(distanceStrNum < 15){
                                    hsWasteSiteVo.setArrivedTime("已达到当前站点");
                                }else {
                                    hsWasteSiteVo.setArrivedTime("预计1分钟内到达");
                                }

                            }else {
                                hsWasteSiteVo.setArrivedTime("步行约"+minutes.longValue()+"分钟");
                            }

                        }

                    }

                }
            }

            TableDataInfo<HsWasteSiteVo> dataResult = new TableDataInfo<HsWasteSiteVo>(dataList, dataList.size());

            return dataResult;
        }

    }


    /**
     * 获取回收站点信息详细信息
     *
     * @param bo
     */
    @SaIgnore
    @GetMapping("/getSiteInfoByIdAndLatAndLon")
    public R<HsWasteSiteVo> getInfo(HsWasteSiteBo bo) {
        if(bo == null || bo.getId() == null){
            return R.fail();
        }

        if(bo.getLongitude() == null || bo.getLatitude() == null){
            return R.ok(iHsWasteSiteService.queryById(bo.getId()));
        }else {
            HsWasteSiteVo hsWasteSiteVo = iHsWasteSiteService.queryByIdAndlatAndLon(bo.getLongitude(), bo.getLatitude(), bo.getId());
            if(hsWasteSiteVo != null && hsWasteSiteVo.getDistance() != null && hsWasteSiteVo.getDistance().compareTo(BigDecimal.ZERO) > 0){

                if(hsWasteSiteVo.getDistance().compareTo(new BigDecimal(20)) >0){
                    hsWasteSiteVo.setDistanceStr(hsWasteSiteVo.getDistance().setScale(0, BigDecimal.ROUND_DOWN)+"公里");
                    hsWasteSiteVo.setArrivedTime("建议驾车前往");
                }else if(hsWasteSiteVo.getDistance().compareTo(new BigDecimal(3)) >0){
                    hsWasteSiteVo.setDistanceStr(hsWasteSiteVo.getDistance().setScale(1, BigDecimal.ROUND_DOWN)+"公里");
                    BigDecimal minutes = hsWasteSiteVo.getDistance().divide(new BigDecimal(0.3),0,BigDecimal.ROUND_DOWN);
                    hsWasteSiteVo.setArrivedTime("骑行预计需要"+minutes.longValue()+"分钟");

                }else {

                    if(hsWasteSiteVo.getDistance().compareTo(new BigDecimal(1)) >=0){
                        hsWasteSiteVo.setDistanceStr(hsWasteSiteVo.getDistance().setScale(1, BigDecimal.ROUND_DOWN)+"公里");
                        BigDecimal minutes = hsWasteSiteVo.getDistance().divide(new BigDecimal(0.1),0,BigDecimal.ROUND_DOWN);
                        hsWasteSiteVo.setArrivedTime("步行预计需要"+minutes.longValue()+"分钟");
                    }else {
                        hsWasteSiteVo.setDistanceStr(hsWasteSiteVo.getDistance().multiply(new BigDecimal(1000)).setScale(0, BigDecimal.ROUND_DOWN)+"米");
                        BigDecimal minutes = hsWasteSiteVo.getDistance().divide(new BigDecimal(0.085),0,BigDecimal.ROUND_DOWN);
                        if(minutes.longValue() <= 0){
                            hsWasteSiteVo.setArrivedTime("预计一分钟内到达");
                        }else {
                            hsWasteSiteVo.setArrivedTime("步行预计需要"+minutes.longValue()+"分钟");
                        }

                    }

                }
            }

            return R.ok(hsWasteSiteVo);
        }

    }










}
